package com.elitedatai.mchtest.map.repository;

import com.elitedatai.mchtest.map.model.entity.GroupEntity;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import java.util.List;
import java.util.Optional;

public interface GroupRepository extends JpaRepository<GroupEntity, Long> {

    @Query("SELECT g FROM GroupEntity g " +
            "WHERE g.workspaceId = :workspaceId " +
            "AND (:groupId IS NULL OR :groupId = '' OR g.groupId = :groupId) " +
            "AND (:isDistributed IS NULL OR g.isDistributed = :isDistributed)")
    List<GroupEntity> findByWorkspaceIdAndOptionalConditions(
            @Param("workspaceId") String workspaceId,
            @Param("groupId") String groupId,
            @Param("isDistributed") Boolean isDistributed
    );

    /**
     * 查找第一个匹配的自定义分组（limit 1）
     */
    Optional<GroupEntity> findFirstByWorkspaceIdAndGroupType(String workspaceId, int groupType);
}
